package com.dd.cloud.user.service.plat.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dd.cloud.common.constants.PlatConstants;
import com.dd.cloud.common.exception.ZekeException;
import com.dd.cloud.common.vo.LoginInfo;
import com.dd.cloud.user.entity.plat.TrustDevice;
import com.dd.cloud.user.mapper.plat.TrustDeviceMapper;
import com.dd.cloud.user.service.plat.ITrustDeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.Duration;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Lx
 * @version 1.0
 */
@Slf4j
@Service
public class TrustDeviceServiceImpl extends ServiceImpl<TrustDeviceMapper, TrustDevice> implements ITrustDeviceService {
    /**
     * 验证信任设备
     *
     * @param phone
     * @param userType
     * @param model
     * @return true 信任 false 为信任待验证
     */
    @Override
    public boolean verifyDevice(String phone, Integer userType, String model) {
        Integer userId = null;
        if (userType == LoginInfo.UserType.DOCTOR) {
            userId = getBaseMapper().queryDoctorUserIdByPhone(phone);
        }
        if (userId == null) {
            throw new ZekeException(400, "手机号不存在");
        }
        TrustDevice td = getTrustDevice(userType, userId, model);
        if (td != null) {
            LocalDateTime now = LocalDateTime.now();
            Duration duration = Duration.between(td.getUpdateDate(), now);
            return duration.toDays() < 30;
        }
        return false;
    }

    /**
     * 查询用户信任设备
     *
     * @param userType
     * @param userId
     * @return
     */
    private TrustDevice getTrustDevice(Integer userType, Integer userId, String model) {
        QueryWrapper<TrustDevice> qw = new QueryWrapper<>();
        qw.lambda().eq(TrustDevice::getUserId, userId);
        qw.lambda().eq(TrustDevice::getUserType, userType);
        qw.lambda().eq(TrustDevice::getModel, model);
        qw.lambda().eq(TrustDevice::getStatus, PlatConstants.TrustDeviceStatus.NORMAL);
        List<TrustDevice> list = list(qw);
        TrustDevice td = null;
        if (list.size() > 1) {
            qw.lambda().ne(TrustDevice::getId, list.get(0).getId());
            TrustDevice update = new TrustDevice();
            update.setStatus(PlatConstants.TrustDeviceStatus.DEL);
            update(update, qw);
        } else if (list.size() == 1) {
            td = list.get(0);
        }
        return td;
    }

    /**
     * 信任设备
     *
     * @param userId
     * @param userType
     * @param model
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void trustDevice(Integer userId, Integer userType, String model, String cid) {
        TrustDevice td = getTrustDevice(userType, userId, model);
        if (td == null) {
            td = new TrustDevice();
            td.setUserId(userId);
            td.setUserType(userType);
            td.setModel(model);
            td.setStatus(PlatConstants.TrustDeviceStatus.NORMAL);
            td.setCreateDate(LocalDateTime.now());
            td.setUpdateDate(td.getCreateDate());
            td.setCid(cid);
            save(td);
        } else {
            TrustDevice update = new TrustDevice();
            update.setId(td.getId());
            update.setUpdateDate(LocalDateTime.now());
            update.setCid(cid);
            updateById(update);
        }
    }

    /**
     * @param type:
     * @return List<String>
     * @description 获取医生的cid
     * @author czg
     * @date 2023/9/18 16:21
     */
    @Override
    public List<String> getCidByDocType(Integer type) {
        return getBaseMapper().queryCidByDocType(type);
    }
}
